from meidou_mall.apps.goods.models import GoodsChannel


def get_categories():
    categories = {}

    channels = GoodsChannel.objects.order_by("sequence").all()
    for channel in channels:
        group_id = channel.group_id

        if group_id not in categories:
            categories[group_id] = {
                "channels": [],
                "sub_cats": []
            }

        cat1 = channel.category
        cat1_dict = {
            "url": channel.url,
            "name": cat1.name,
        }

        categories[group_id]["channels"].append(cat1_dict)

        cats2 = cat1.subs.all()
        # for cat2 in cats2:
        #     categories[group_id]["sub_cats"].append({
        #         'name': cat2.name,
        #         'subs': cat2.subs.all()
        #     })

        for cat2 in cats2:
            subs_list = []
            subs_data = cat2.subs.all()
            for sub in subs_data:
                subs_list.append({
                    "id": sub.id,
                    "name": sub.name
                })
            categories[group_id]["sub_cats"].append({
                'name': cat2.name,
                'subs': subs_list
            })

    return categories